package com.service;

import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.urule.console.DefaultPrincipal;
import com.bstek.urule.console.repository.Repository;
import com.bstek.urule.console.repository.RepositoryService;
import com.bstek.urule.console.repository.RepositoryServiceImpl;
import com.bstek.urule.console.repository.model.FileType;
import com.bstek.urule.console.repository.model.RepositoryFile;
import com.bstek.urule.console.repository.model.ResourceItem;
import com.bstek.urule.console.repository.model.ResourcePackage;
import com.bstek.urule.console.repository.model.Type;
import com.dao.RfileDao;
import com.entity.RuleFile;

@Component
public class RfilePR {

	@Resource(name = RepositoryService.BEAN_ID)
	private RepositoryService uRuleService;

	@Autowired
	private RfileDao rfileDao;

	@DataProvider
	public List<RuleFile> loadRuleFile() {
		return rfileDao.loadRuleFile();
	}

	@DataProvider
	public List<RuleFile> loadMbRuleFile() throws Exception {
		List<RuleFile> retuenValue = new ArrayList<RuleFile>();
		List<RepositoryFile> projectFile = this.loadProject();// 获取项目文件
		for (RepositoryFile repFile : projectFile) {
			if (repFile.getName().equals("决策表")) {
				if (repFile.getChildren() != null) {
					for (RepositoryFile repFileChild : repFile.getChildren()) {
						RuleFile rulefile = new RuleFile();
						rulefile.setFileName(repFileChild.getName());
						rulefile.setFileType(0);
						rulefile.setFilePath(repFileChild.getFullPath());
						retuenValue.add(rulefile);
					}
				}
			} else if (repFile.getName().equals("决策集")) {
				if (repFile.getChildren() != null) {
					for (RepositoryFile repFileChild : repFile.getChildren()) {
						RuleFile rulefile = new RuleFile();
						rulefile.setFileName(repFileChild.getName());
						rulefile.setFileType(1);
						rulefile.setFilePath(repFileChild.getFullPath());
						retuenValue.add(rulefile);
					}
				}
			} else if (repFile.getName().equals("决策树")) {
				if (repFile.getChildren() != null) {
					for (RepositoryFile repFileChild : repFile.getChildren()) {
						RuleFile rulefile = new RuleFile();
						rulefile.setFileName(repFileChild.getName());
						rulefile.setFileType(2);
						rulefile.setFilePath(repFileChild.getFullPath());
						retuenValue.add(rulefile);
					}
				}
			} else if (repFile.getName().equals("评分卡")) {
				if (repFile.getChildren() != null) {
					for (RepositoryFile repFileChild : repFile.getChildren()) {
						RuleFile rulefile = new RuleFile();
						rulefile.setFileName(repFileChild.getName());
						rulefile.setFileType(3);
						rulefile.setFilePath(repFileChild.getFullPath());
						retuenValue.add(rulefile);
					}
				}
			} else if (repFile.getName().equals("决策流")) {
				if (repFile.getChildren() != null) {
					for (RepositoryFile repFileChild : repFile.getChildren()) {
						RuleFile rulefile = new RuleFile();
						rulefile.setFileName(repFileChild.getName());
						rulefile.setFileType(4);
						rulefile.setFilePath(repFileChild.getFullPath());
						retuenValue.add(rulefile);
					}
				}
			}
		}
		return retuenValue;
	}

	/**
	 * 保存模板文件
	 * 
	 * @param parameter
	 * @throws Exception
	 */
	@Expose
	public String saveMbFile(Map<String, Object> parameter) throws Exception {
		String filename = parameter.get("filename").toString();

		int mbFiletype = (int) parameter.get("mbFiletype");
		String mbFilepath = parameter.get("mbFilepath").toString();

		InputStream inputStream = uRuleService.readFile(mbFilepath);
		String content = IOUtils.toString(inputStream);

		List<RepositoryFile> projectFile = this.loadProject();// 获取项目文件
		String projectName = uRuleService.getProject(projectFile.get(0).getFullPath());
		String urlFilename = URLEncoder.encode(projectName, "utf-8");
		if (mbFiletype == 0) {
			filename = filename + ".dt.xml";
			urlFilename += "/" + filename;
			uRuleService.createFile(projectName + "/" + filename, content, this.getDefaultPrincipal());
			rfileDao.saveNewFile(filename, 0, urlFilename);
		}
		if (mbFiletype == 1) {
			filename = filename + ".rs.xml";
			urlFilename += "/" + filename;
			uRuleService.createFile(projectName + "/" + filename, content, this.getDefaultPrincipal());
			rfileDao.saveNewFile(filename, 1, urlFilename);
		}
		if (mbFiletype == 2) {
			filename = filename + ".dtree.xml";
			urlFilename += "/" + filename;
			uRuleService.createFile(projectName + "/" + filename, content, this.getDefaultPrincipal());
			rfileDao.saveNewFile(filename, 2, urlFilename);
		}
		if (mbFiletype == 3) {
			filename = filename + ".sc";
			urlFilename += "/" + filename;
			uRuleService.createFile(projectName + "/" + filename, content, this.getDefaultPrincipal());
			rfileDao.saveNewFile(filename, 3, urlFilename);
		}
		if (mbFiletype == 4) {
			filename = filename + ".rl.xml";
			urlFilename += "/" + filename;
			uRuleService.createFile(projectName + "/" + filename, content, this.getDefaultPrincipal());
			rfileDao.saveNewFile(filename, 4, urlFilename);
		}
		return urlFilename;
	}

	/**
	 * 获取项目资源树，并以数组形式返回结果
	 * 
	 * @return
	 */
	public List<RepositoryFile> loadProject() {
		DefaultPrincipal user = this.getDefaultPrincipal();

		List<RepositoryFile> fileResult = new ArrayList<RepositoryFile>();
		try {
			List<RepositoryFile> repositoryFiles = uRuleService.loadProjects("lcx");// 根据公司id获取公司下的项目

			for (RepositoryFile rFile : repositoryFiles) {
				String project = uRuleService.getProject(rFile.getFullPath());// 把项目路径转换成项目名称

				Repository repository = uRuleService.loadRepository(project, user, true, FileType.values(), null);

				if (repository.getRootFile().getChildren() != null) {
					for (RepositoryFile rf : repository.getRootFile().getChildren()) {
						fileResult.addAll(this.seachChildren(rf));
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileResult;
	}

	/**
	 * 遍历树
	 * 
	 * @param childrenFile
	 * @return
	 */
	private List<RepositoryFile> seachChildren(RepositoryFile childrenFile) {
		List<RepositoryFile> fileResult = new ArrayList<RepositoryFile>();
		if (childrenFile.getChildren() != null) {
			for (RepositoryFile repositoryFile : childrenFile.getChildren()) {
				fileResult.add(repositoryFile);
				if (repositoryFile.getChildren() != null) {
					for (RepositoryFile rfChildren : repositoryFile.getChildren()) {
						fileResult.add(rfChildren);
						this.seachChildren(rfChildren);
					}
				}
			}
		}
		return fileResult;
	}

	/**
	 * 创建规则文件
	 * 
	 * @param type
	 */
	@Expose
	public String createRuleFile(String type) {
		DefaultPrincipal user = this.getDefaultPrincipal();

		List<RepositoryFile> projectFile = this.loadProject();// 获取项目文件

		String projectName = uRuleService.getProject(projectFile.get(0).getFullPath());
		String filename = null;// 新建文件名
		String urlFilename = null;// 返回前台的url的路径名

		List<RepositoryFile> kuChildren = this.getFileContentByType(projectFile, "库");// 获取库文件
		String importPath = null;// 规则文件源码中 导入库文件的 源码片段
		StringBuffer stBufferKu = new StringBuffer();// 将导入库文件的源码追加起来
		for (RepositoryFile kufile : kuChildren) {
			if (kufile.getType() == Type.variable) {// 变量库
				importPath = "<import-variable-library path=\"jcr:" + kufile.getFullPath() + "\"/> ";
			} else if (kufile.getType() == Type.parameter) {// 参数库
				importPath = "<import-parameter-library path=\"jcr:" + kufile.getFullPath() + "\"/> ";
			} else if (kufile.getType() == Type.constant) {// 常量库
				importPath = "<import-constant-library path=\"jcr:" + kufile.getFullPath() + "\"/> ";
			} else if (kufile.getType() == Type.action) {// 动作库
				importPath = "<import-action-library path=\"jcr:" + kufile.getFullPath() + "\"/> ";
			}
			stBufferKu.append(importPath);
		}
		try {
			if ("dt".equals(type)) {
				int num = this.getFileSizeByType(projectFile, "决策表");// 当前有多少决策表
				StringBuffer stBufferJcb = new StringBuffer();
				stBufferJcb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<decision-table> "
						+ "<remark><![CDATA[]]></remark>" + stBufferKu + "<cell row=\"0\" col=\"0\" rowspan=\"1\">"
						+ "<joint type=\"and\">" + "<condition op=\"Equals\"> "
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/>"
						+ "</condition>" + "</joint>" + "</cell>" + "<cell row=\"0\" col=\"1\" rowspan=\"1\">"
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/> " + "</cell> "
						+ "<cell row=\"1\" col=\"0\" rowspan=\"1\">" + "<joint type=\"and\">"
						+ "<condition op=\"Equals\">"
						+ "<value const-category=\"性别\" const=\"1\" const-label=\"男\" type=\"Constant\"/>"
						+ "</condition> " + "</joint>" + "</cell>" + "<cell row=\"1\" col=\"1\" rowspan=\"1\"> "
						+ "<value const-category=\"性别\" const=\"1\" const-label=\"男\" type=\"Constant\"/> "
						+ "</cell>  " + "<row num=\"0\" height=\"40\"/>" + "<row num=\"1\" height=\"40\"/>"
						+ "<col num=\"0\" width=\"120\" type=\"Criteria\" var-category=\"人对象\" var-label=\"sex\" var=\"sex\" datatype=\"Integer\"/> "
						+ "<col num=\"1\" width=\"200\" type=\"Assignment\" var-category=\"参数\" var-label=\"参数1\" var=\"param1\" datatype=\"String\"/>"
						+ "</decision-table>");
				urlFilename = URLEncoder.encode(projectName, "utf-8");
				filename = "NewFileJcb" + (num + 1) + ".dt.xml";
				urlFilename += "/" + filename;
				uRuleService.createFile(projectName + "/" + filename, stBufferJcb.toString(), user);
				rfileDao.saveNewFile(filename, 0, urlFilename);
			} else if ("rs".equals(type)) {
				int num = this.getFileSizeByType(projectFile, "决策集");// 当前有多少决策集
				StringBuffer stBufferJcj = new StringBuffer();
				stBufferJcj.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<rule-set>" + stBufferKu
						+ "<remark><![CDATA[]]></remark>" + " <rule name=\"rule1\">" + "<remark><![CDATA[]]></remark>"
						+ " <if>" + " <and>" + "<atom op=\"Equals\">"
						+ "<left var-category=\"人对象\" var=\"sex\" var-label=\"sex\" datatype=\"Integer\" type=\"variable\"/>"
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/>" + "</atom>"
						+ "</and>" + "</if>" + "<then>"
						+ "<var-assign var-category=\"参数\" var=\"param1\" var-label=\"参数1\" datatype=\"String\" type=\"parameter\">"
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/>"
						+ "</var-assign>" + "</then>" + "<else>"
						+ "<var-assign var-category=\"参数\" var=\"param2\" var-label=\"参数2\" datatype=\"String\" type=\"parameter\">"
						+ "<value const-category=\"性别\" const=\"1\" const-label=\"男\" type=\"Constant\"/>"
						+ "</var-assign>" + "</else>" + "</rule>" + "</rule-set>");
				urlFilename = URLEncoder.encode(projectName, "utf-8");
				filename = "NewFileJcj" + (num + 1) + ".rs.xml";
				urlFilename += "/" + filename;
				uRuleService.createFile(projectName + "/" + filename, stBufferJcj.toString(), user);
				rfileDao.saveNewFile(filename, 1, urlFilename);
			} else if ("dtree".equals(type)) {
				int num = this.getFileSizeByType(projectFile, "决策树");// 当前有多少决策树
				StringBuffer stBufferJcs = new StringBuffer();
				stBufferJcs.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<decision-tree>"
						+ "<remark><![CDATA[]]></remark>" + stBufferKu + "<variable-tree-node>"
						+ "<left var-category=\"人对象\" var=\"sex\" var-label=\"sex\" datatype=\"Integer\" type=\"variable\"/>"
						+ "<condition-tree-node op=\"Equals\">"
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/>"
						+ "<action-tree-node>"
						+ "<var-assign var-category=\"参数\" var=\"param1\" var-label=\"参数1\" datatype=\"String\" type=\"parameter\">"
						+ "<value const-category=\"性别\" const=\"0\" const-label=\"女\" type=\"Constant\"/>"
						+ "</var-assign>" + "</action-tree-node>" + "</condition-tree-node>"
						+ "<condition-tree-node op=\"Equals\">"
						+ "<value const-category=\"性别\" const=\"1\" const-label=\"男\" type=\"Constant\"/>"
						+ "<action-tree-node>"
						+ "<var-assign var-category=\"参数\" var=\"param2\" var-label=\"参数2\" datatype=\"String\" type=\"parameter\">"
						+ "<value const-category=\"性别\" const=\"1\" const-label=\"男\" type=\"Constant\"/>"
						+ "</var-assign>" + "</action-tree-node>" + "</condition-tree-node>" + "</variable-tree-node>"
						+ "</decision-tree>");
				urlFilename = URLEncoder.encode(projectName, "utf-8");
				filename = "NewFileJcs" + (num + 1) + ".dtree.xml";
				urlFilename += "/" + filename;
				uRuleService.createFile(projectName + "/" + filename, stBufferJcs.toString(), user);
				rfileDao.saveNewFile(filename, 2, urlFilename);
			} else if ("sc".equals(type)) {
				int num = this.getFileSizeByType(projectFile, "评分卡");// 当前有多少评分卡
				StringBuffer stBufferPfk = new StringBuffer();
				stBufferPfk.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
						+ "<scorecard weight-support=\"false\" name=\"评分卡1\" attr-col-width=\"200\" attr-col-name=\"属性\" attr-col-category=\"人对象\" condition-col-width=\"220\" condition-col-name=\"条件\" score-col-width=\"180\" score-col-name=\"分值\" scoring-type=\"sum\" assign-target-type=\"parameter\" var-category=\"参数\" var=\"param1\" var-label=\"参数1\" datatype=\"String\">"
						+ "<remark><![CDATA[]]></remark>" + stBufferKu
						+ "<card-cell type=\"attribute\" row=\"2\" col=\"1\" var=\"year\" var-label=\"year\" datatype=\"Integer\"/>"
						+ "<card-cell type=\"condition\" row=\"2\" col=\"2\"> " + "<joint type=\"and\">"
						+ "<condition op=\"GreaterThen\">" + "<value content=\"18\" type=\"Input\"/>" + "</condition>"
						+ "</joint>" + "</card-cell>" + "<card-cell type=\"score\" row=\"2\" col=\"3\">"
						+ "<value content=\"1\" type=\"Input\"/>" + "</card-cell>"
						+ "<card-cell type=\"condition\" row=\"3\" col=\"2\">" + "<joint type=\"and\">"
						+ "<condition op=\"GreaterThenEquals\">" + "<value content=\"18\" type=\"Input\"/>"
						+ "</condition>" + "<condition op=\"LessThen\">" + "<value content=\"40\" type=\"Input\"/>"
						+ "</condition>" + "</joint>" + "</card-cell>"
						+ "<card-cell type=\"score\" row=\"3\" col=\"3\">" + "<value content=\"2\" type=\"Input\"/>"
						+ "</card-cell>" + "<card-cell type=\"condition\" row=\"4\" col=\"2\">" + "<joint type=\"and\">"
						+ "<condition op=\"GreaterThenEquals\">" + "<value content=\"40\" type=\"Input\"/>"
						+ "</condition>" + "<condition op=\"LessThen\">" + "<value content=\"70\" type=\"Input\"/>"
						+ "</condition>" + "</joint>" + "</card-cell>"
						+ "<card-cell type=\"score\" row=\"4\" col=\"3\">" + "<value content=\"3\" type=\"Input\"/>"
						+ "</card-cell>" + "<card-cell type=\"condition\" row=\"5\" col=\"2\">" + "<joint type=\"and\">"
						+ "<condition op=\"GreaterThenEquals\">" + "<value content=\"70\" type=\"Input\"/>"
						+ "</condition>" + "</joint>" + "</card-cell>"
						+ "<card-cell type=\"score\" row=\"5\" col=\"3\">" + "<value content=\"4\" type=\"Input\"/>"
						+ "</card-cell>" + "<attribute-row row-number=\"2\">" + "<condition-row row-number=\"3\"/>"
						+ "<condition-row row-number=\"4\"/>" + "<condition-row row-number=\"5\"/>" + "</attribute-row>"
						+ "</scorecard>");
				urlFilename = URLEncoder.encode(projectName, "utf-8");
				filename = "NewFilePfk" + (num + 1) + ".sc";
				urlFilename += "/" + filename;
				uRuleService.createFile(projectName + "/" + filename, stBufferPfk.toString(), user);
				rfileDao.saveNewFile(filename, 3, urlFilename);
			} else if ("rl".equals(type)) {
				int num = this.getFileSizeByType(projectFile, "决策流");// 当前有多少决策流
				StringBuffer stBufferJcl = new StringBuffer();
				stBufferJcl
						.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<rule-flow id=\"liu1\" debug=\"false\">"
								+ stBufferKu + "<start name=\"开始1\" x=\"249\" y=\"36\" width=\"51\" height=\"80\">"
								+ "<connection g=\"\" type=\"line\" to=\"规则1\"/>" + "</start>"
								+ "<rule name=\"规则1\" x=\"424\" y=\"35\" width=\"51\" height=\"80\" file=\"jcr:/实例项目/bbbbb.rs.xml\" version=\"LATEST\">"
								+ "<connection g=\"\" type=\"line\" to=\"规则2\"/>" + "</rule>"
								+ "<rule name=\"规则2\" x=\"430\" y=\"218\" width=\"51\" height=\"80\" file=\"jcr:/实例项目/aaaa.ul\" version=\"LATEST\"/>"
								+ "</rule-flow>");
				urlFilename = URLEncoder.encode(projectName, "utf-8");
				filename = "NewFileJcl" + (num + 1) + ".rl.xml";
				urlFilename += "/" + filename;
				uRuleService.createFile(projectName + "/" + filename, stBufferJcl.toString(), user);
				rfileDao.saveNewFile(filename, 4, urlFilename);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return urlFilename;
	}

	/**
	 * 删除数据库文件、同时删除规则文件
	 * 
	 * @param path
	 * @param fileId
	 */
	@Expose
	public void deleteRuleFile(String path, String fileId) {
		DefaultPrincipal user = this.getDefaultPrincipal();
		try {
			String dPath = URLDecoder.decode(path, "utf-8");
			uRuleService.deleteFile(dPath, user);
			rfileDao.deleteRuleFile(fileId);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	/**
	 * 将文件添加到知识包
	 * 
	 * @param files
	 */
	@Expose
	public List<String> ajaxActionPackage(List<RuleFile> files, String packageId) {
		List<String> errorValue = new ArrayList<String>();
		for (RuleFile file : files) {
			if (this.checkPackageHaveFileByPackageId(file.getFilePath(), packageId)) {
				errorValue.add(file.getFileName());
			}
		}
		if (errorValue.size() == 0) {
			try {
				DefaultPrincipal user = this.getDefaultPrincipal();
				List<RepositoryFile> repositoryFiles = uRuleService.loadProjects("lcx");
				String project = uRuleService.getProject(repositoryFiles.get(0).getFullPath());// 把项目路径转换成项目名称
				List<ResourcePackage> knowledgedPackages = uRuleService.loadProjectResourcePackages(project);// 根据项目名称获取该项目的下知识包
				ResourcePackage currentPackage = new ResourcePackage();
				String path = URLEncoder.encode(project, "utf-8") + "/" + RepositoryServiceImpl.RES_PACKGE_FILE;
				SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				StringBuffer xml = new StringBuffer();
				xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<res-packages>");
				for (ResourcePackage rpackage : knowledgedPackages) {
					StringBuffer xmlBuffer = new StringBuffer();
					if (rpackage.getId().equals(packageId)) {
						StringBuffer fileBuffer = new StringBuffer();
						currentPackage = rpackage;
						for (RuleFile rfile : files) {
							fileBuffer.append("<res-package-item  name=\"" + rfile.getFileName() + "\" path=\""
									+ rfile.getFilePath() + "\" version=\"LATEST\"/>");
						}
						for (ResourceItem item : currentPackage.getResourceItems()) {
							fileBuffer.append("<res-package-item  name=\"" + item.getName() + "\" path=\""
									+ item.getPath() + "\" version=\"" + item.getVersion() + "\"/>");
						}
						xmlBuffer.append("<res-package id=\"" + currentPackage.getId() + "\" name=\""
								+ currentPackage.getName() + "\" create_date=\""
								+ sd.format(currentPackage.getCreateDate()) + "\" >" + fileBuffer + "</res-package>");
					} else {
						if (rpackage.getResourceItems() != null) {
							StringBuffer fileBuffer = new StringBuffer();
							for (ResourceItem item : rpackage.getResourceItems()) {
								fileBuffer.append("<res-package-item  name=\"" + item.getName() + "\" path=\""
										+ item.getPath() + "\" version=\"" + item.getVersion() + "\"/>");
							}
							xmlBuffer.append("<res-package id=\"" + rpackage.getId() + "\" name=\"" + rpackage.getName()
									+ "\" create_date=\"" + sd.format(rpackage.getCreateDate()) + "\" >" + fileBuffer
									+ "</res-package>");
						}
					}
					xml.append(xmlBuffer);
				}
				xml.append("</res-packages>");
				// String xml = "<?xml version=\"1.0\"
				// encoding=\"utf-8\"?><res-packages></res-packages>";
				uRuleService.saveFile(path, xml.toString(), false, (String) null, user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return errorValue;
	}

	/**
	 * 确认知识包中是否存在某文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @return
	 */
	@Expose
	public boolean checkPackageHaveFile(String filePath) {
		try {
			List<RepositoryFile> repositoryFiles = uRuleService.loadProjects("lcx");
			String project = uRuleService.getProject(repositoryFiles.get(0).getFullPath());// 把项目路径转换成项目名称
			List<ResourcePackage> knowledgedPackages = uRuleService.loadProjectResourcePackages(project);// 根据项目名称获取该项目的下知识包
			if (!knowledgedPackages.isEmpty()) {
				for (ResourcePackage kpackage : knowledgedPackages) {
					if (kpackage.getResourceItems() != null) {
						for (ResourceItem items : kpackage.getResourceItems()) {
							if (items.getPath().equals(filePath)) {
								return true;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 根据知识包Id确认该知识包中是否存在某文件
	 * 
	 * @param filePath
	 * @param packageId
	 * @return
	 */
	public boolean checkPackageHaveFileByPackageId(String filePath, String packageId) {
		try {
			List<RepositoryFile> repositoryFiles = uRuleService.loadProjects("lcx");
			String project = uRuleService.getProject(repositoryFiles.get(0).getFullPath());// 把项目路径转换成项目名称
			List<ResourcePackage> knowledgedPackages = uRuleService.loadProjectResourcePackages(project);// 根据项目名称获取该项目的下知识包
			if (!knowledgedPackages.isEmpty()) {
				for (ResourcePackage kpackage : knowledgedPackages) {
					if (kpackage.getId().equals(packageId)) {
						if (kpackage.getResourceItems() != null) {
							for (ResourceItem items : kpackage.getResourceItems()) {
								if (items.getPath().equals(filePath)) {
									return true;
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 根据文件类型返回该文件类型数量
	 * 
	 * @param projectFile
	 *            项目文件(loadProject方法的返回值)
	 * @param type
	 *            文件类型
	 * @return
	 */
	private int getFileSizeByType(List<RepositoryFile> projectFile, String type) {
		int size = 0;
		for (RepositoryFile rf : projectFile) {
			if (rf.getName().equals(type)) {
				if (rf.getChildren() != null) {
					size = rf.getChildren().size();
				}
			}
		}
		return size;
	}

	/**
	 * 根据文件类型返回该文件类型中的内容
	 * 
	 * @param projectFile
	 * @param type
	 * @return
	 */
	private List<RepositoryFile> getFileContentByType(List<RepositoryFile> projectFile, String type) {
		List<RepositoryFile> childrenFile = new ArrayList<RepositoryFile>();
		for (RepositoryFile rf : projectFile) {
			if (rf.getName().equals(type)) {
				childrenFile = rf.getChildren();
			}
		}
		return childrenFile;
	}

	/**
	 * 初始化规则用户
	 * 
	 * @return
	 */
	private DefaultPrincipal getDefaultPrincipal() {
		DefaultPrincipal principal = new DefaultPrincipal();
		principal.setCompanyId(ContextHolder.getLoginUser().getCompanyId());
		principal.setName(ContextHolder.getLoginUserName());
		principal.setDisplayName(ContextHolder.getLoginUser().getCname());
		return principal;
	}

	public void setService(RepositoryService service) {
		this.uRuleService = service;
	}
}
